package com.cloud.flowbusdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.cloud.flowbusdemo.databinding.ActivityMainBinding
import com.cloud.flowbusdemo.flow.FlowBus
import com.cloud.flowbusdemo.http.HttpUtils
import com.cloud.flowbusdemo.intent.MainIntent
import com.cloud.flowbusdemo.model.MessageEvent
import com.cloud.flowbusdemo.service.FlowBusTestService
import com.cloud.flowbusdemo.ui.adapter.WallpaperAdapter
import com.cloud.flowbusdemo.ui.viewmodel.MainViewModel
import com.cloud.flowbusdemo.ui.viewmodel.ViewModelFactory
import com.cloud.flowbusdemo.uistate.MainUIState
import com.cloud.flowbusdemo.utils.CToast
import com.cloud.flowbusdemo.utils.GenericToast
import com.cloud.flowbusdemo.utils.SingleToast
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    private lateinit var mainViewModel: MainViewModel

    private var wallPaperAdapter = WallpaperAdapter(arrayListOf())
    private val TAG = "flowBusDemo"

    private var mCToast: CToast? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mainViewModel = ViewModelProvider(
            this,
            ViewModelFactory(HttpUtils.apiService)
        )[MainViewModel::class.java]
        initView()
        observeViewModel()
        initService()
    }

    private fun initService() {
        val intent = Intent(this@MainActivity, FlowBusTestService::class.java)
        intent.putExtra("sockUrl","")
        startService(intent)
    }

    /**
     * ViewModel
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun observeViewModel() {
        lifecycleScope.launch {
            mainViewModel.state.collect {
                when (it) {
                    is MainUIState.Idle -> {

                    }

                    is MainUIState.Loading -> {
                        binding.btnGetWallpaper.visibility = View.GONE
                        binding.pbLoading.visibility = View.VISIBLE
                    }

                    is MainUIState.Success -> {     //数据返回
                        binding.btnGetWallpaper.visibility = View.GONE
                        binding.pbLoading.visibility = View.GONE

                        binding.rvWallpaper.visibility = View.VISIBLE
                        it.wallpaper.let { paper ->
                            wallPaperAdapter.addData(paper.res.vertical)
                        }
                        wallPaperAdapter.notifyDataSetChanged()
                    }

                    is MainUIState.Error -> {
                        binding.pbLoading.visibility = View.GONE
                        binding.btnGetWallpaper.visibility = View.VISIBLE
                        Log.d("TAG", "observeViewModel: $it.error")
                        Toast.makeText(this@MainActivity, it.error, Toast.LENGTH_LONG).show()
                    }
                }
            }
        }
    }

    /**
     * 初始化
     */
    private fun initView() {
        binding.rvWallpaper.apply {
            layoutManager = GridLayoutManager(this@MainActivity, 2)
            adapter = wallPaperAdapter
        }
        binding.btnGetWallpaper.setOnClickListener {
           lifecycleScope.launch {
                mainViewModel.mainIntentChannel.send(MainIntent.GetWallpaper)
            }
            val intent = Intent(this@MainActivity,TestActivity::class.java)
            startActivity(intent)


            val timeToast =
                SingleToast.makeText(this@MainActivity, "显示时间自定的Toast", 10.0)
            timeToast.show()
        }
        FlowBus.with<MessageEvent>("test").register(this@MainActivity) {
            LogUtils.d(TAG,it.toString())
            if(it.message == "stop"){
                LogUtils.d(TAG,"===接收到的消息为==="+it.message)
            }
        }
        FlowBus.with<MessageEvent>("mineFragment").register(this@MainActivity) {
            LogUtils.d(TAG,it.toString())
            if(it.message == "onMine"){
                LogUtils.d(TAG,"===接收到的消息为1111==="+it.message)
            }
        }
    }
}